<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\KeyMap;
use app\models\UserBill;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $user app\models\User
 * @var $billList app\models\UserBill[] 账单列表
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '用户账单列表';
$this->params['breadcrumbs'][] = '用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>

<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th colspan="7">当前用户：<?php echo Html::a(Html::encode($user->username), ['/admin/user/user-view', 'id' => $user->id]);?></th>
    </tr>
    <tr>
        <th>月份</th>
        <th>套餐</th>
        <th>计费数量</th>
        <th>单价</th>
        <th>金额</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($billList as $bill) {?>
        <tr>
            <td class="center"><?php echo Html::encode($bill->month);?></td>
            <td><?php echo Html::encode($bill->package->name);?></td>
            <th><?php echo $bill->card_amount;?></th>
            <th><?php echo $bill->price;?></th>
            <td><?php echo $bill->money;?></td>
            <td><?php echo KeyMap::getValue('user_bill_status', $bill->status);?></td>
            <td><?php echo ManagerTableOp::widget(['items' => [
                    ($bill->status == UserBill::STATUS_EMPTY) ?: ['icon' => 'fa fa-info-circle', 'href' => Url::to(['/admin/card/bill', 'search_month' => $bill->month, 'search_username' => $user->username, 'search_package_id' => $bill->package_id]), 'btn_class' => 'btn btn-xs btn-default', 'tip' => '账单详情'],
                    !($bill->status == UserBill::STATUS_WAIT_CONFIRM) ?: ['icon' => 'fa fa-pencil', 'onclick' => 'modifyMoney(' . $bill->id . ', \'' . $bill->money . '\')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '调整金额', 'color' => 'yellow'],
                    !($bill->status == UserBill::STATUS_WAIT_CONFIRM) ?: ['icon' => 'fa fa-check', 'onclick' => 'publishBill(' . $bill->id . ')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '发布账单', 'color' => 'yellow'],
                    !($bill->status == UserBill::STATUS_PUBLISH) ?: ['icon' => 'fa fa-file-excel-o', 'href' => Url::to(['/admin/card/bill', 'export'=>'excel', 'search_month' => $bill->month, 'search_username' => $user->username, 'search_package_id' => $bill->package_id]), 'btn_class' => 'btn btn-xs btn-success', 'tip' => '导出Excel文件', 'color' => 'green'],
                ]]);?>
            </td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<script>
    /**
     * 调整账单金额
     * @param id
     * @param money
     */
    function modifyMoney(id, money) {
        var newMoney = window.prompt('请输入新的账单金额：', money);
        $.getJSON('<?php echo Url::to(['/admin/user/modify-bill-money']);?>', {id: id, money: newMoney}, function (json) {
            if (callback(json)) {
                layer.msg('金额已更新。', function () {window.location.reload();});
            }
        });
    }

    /**
     * 发布账单
     * @param id
     */
    function publishBill(id) {
        if (!confirm('确定要发布账单吗？')) {
            return false;
        }
        $.getJSON('<?php echo Url::to(['/admin/user/publish-bill']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('账单已发布。', function () {window.location.reload();});
            }
        });
    }
</script>
